#include <Rock.h>

#include <RenderObject.h>
#include <GraphicData.h>
#include <RenderManager.h>
#include <PhysicsObject.h>
#include <Physics.h>
#include <ObjectManager.h>

Rock::Rock()
{
	float rockColor[4] = {1,0,0,1};
	gfx = External::renderer->CreateRenderObject( &t, External::renderer->LoadGfx( "./Graphics/rock.gfx" ) );
	//memcpy( gfx->color, rockColor, sizeof(float)*4 );
	for(unsigned i = 0; i < gfx->colors.size(); ++i)
		gfx->SetColor(i, rockColor[0], rockColor[1], rockColor[2]);
	gfx->gfx->solid[0] = 1;
	gfx->gfx->solid[1] = 1;

	t.loc = vec2(100,100);
	t.angle = 360.0f * Global::GetRandomFloat();

	//velocity = vec2( -10 + 20*Global::GetRandomFloat(), -10 + 20*Global::GetRandomFloat() );


	phys = new PhysicsObject( this, &t, External::physics->LoadBoundingBox("./Graphics/rock.bb") );
	phys->body->SetTransform( t.loc, -t.angle * Global::PI / 180.0f );
	//phys->body->SetLinearVelocity( velocity );
}

Rock::~Rock()
{
	delete phys;
}
	
std::string Rock::GetTypeName()
{
	return "Rock";
}
	
std::string Rock::GetTextName()
{
	return "Rock";
}

void Rock::Update(float dt)
{
	if(!phys->body->IsAwake())
		return;

	SyncTransformToPhysics();

	if( t.loc.x < 0 )
		phys->body->SetTransform( vec2(Global::GameWorldSize.x,t.loc.y), -t.GetAngleRadians() );
	if( t.loc.x > Global::GameWorldSize.x )
		phys->body->SetTransform( vec2(0,t.loc.y), -t.GetAngleRadians() );
	if( t.loc.y < 0 )
		phys->body->SetTransform( vec2(t.loc.x,Global::GameWorldSize.y), -t.GetAngleRadians() );
	if( t.loc.y > Global::GameWorldSize.y )
		phys->body->SetTransform( vec2(t.loc.x,0), -t.GetAngleRadians() );
}
		
void Rock::DoCollision(BaseObject* objectCollidedWith)
{
	//std::cout<<"rock at "<<t.loc.x <<" ,"<<t.loc.y <<" collided with a "<<objectCollidedWith->GetTypeName() <<" at "<<objectCollidedWith->t.loc.x <<" ,"<<objectCollidedWith->t.loc.y <<"\n";

	if( objectCollidedWith->GetTypeName() == "EnemyShip" )
	{
		//External::objects->RemoveObject( objectCollidedWith );
	}
}
